package com.xxz.terminal;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class WelcomActivity extends AppCompatActivity implements Runnable {

    private static final String TAG = "run";
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);

        //开启子线程
        Thread t = new Thread(this);
        t.start();

        TextView show = findViewById(R.id.textgs);

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(Message msg) {

                if(msg.what==7){

                    String str = (String) msg.obj;
                    Log.i(TAG, "handleMessage: getMessage msg = " + str);
                    show.setText(str);

                }
                super.handleMessage(msg);
            }
        };

        //实现从欢迎页面到下一个界面的延迟跳转
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                SharedPreferences sp = getSharedPreferences("userinformation",MODE_PRIVATE);
                String password = sp.getString("password",null);
                if(password == null){

                    Intent LoginPageIntent = new Intent(WelcomActivity.this, LoginActivity.class);
                    startActivity(LoginPageIntent);
                    finish();

                }
                else {

                    Intent MainPageIntent = new Intent(WelcomActivity.this, MainPageActivity.class);
                    startActivity(MainPageIntent);
                    finish();

                }//实现当已经有账号时跳过登录注册界面，直接进入主界面

            }
        },3000);//3000毫秒后执行，即3秒跳转
    }

//通过访问“一言”这个句子接口获取欢迎页面展示话语
    @Override
    public void run() {

        Log.i(TAG, "run: run()......");
        String content = new String();
        String value = new String();

        URL url = null;
        try {

            url = new URL("https://v1.hitokoto.cn?c=e");//“一言”的网址
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();//与该网址建立联系
            connection.setRequestMethod("GET");//设置访问方式
            connection.connect();//建立连接

            int responsecode = connection.getResponseCode();
            Log.i("re","response"+responsecode);

            if(responsecode == HttpURLConnection.HTTP_OK){

                InputStream in = connection.getInputStream();//获取数据流

                InputStreamToString convert = new InputStreamToString();
                content = convert.InputStreamToString(in);//调用InputStreamToString方法将数据流转化为字符串
                Log.i(TAG,"run: content"+content);

                JSONObject jsonObject = new JSONObject(content);//将得到的字符串转化为JSONObject类型以方便通过接口定义的“hitokoto”字段名直接取出正文
                value = jsonObject.optString("hitokoto");//取出正文即句子将其转化为字符串
                Log.i("value","value"+value);
            }

        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }

        //获取Msg对象，用于返回主线程
          Message msg = handler.obtainMessage(7,value);
          handler.sendMessage(msg);
          Log.i(TAG, "run: sendMessage ok");

    }
}
